// pages/home/home.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    indexes:0,
    indexs:0,
    classans:{class0:"answer",class1:"answer",class2:"answer",class3:"answer"},
    // item:{},
    ans:0,
    correct:0,
    wrong:0,
    array: {
      Question: '',
      answer: [],
      correctanswer: ''
    },
     Question:{},
     Answer:{},
     end:0,
     now:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    this.setData({
      indexs: app.globalData.indexs
    });
    wx.request({
      url: 'https://tally.slickghost.com/demo2/getProblem',
      data: { level: app.globalData.lever },
      method: 'get',
      success: function (res) {
        var array = {
          Question: '',
          answer: [],
          correctanswer: ''
        };
        array.Question = res.data[0]+"=?";
        for(var i=2;i<6;i++)
        {
          array.answer.push(res.data[i]);
        }
        array.correctanswer = res.data[1];
        that.setData({
          array: array
        });
      }

    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  submit: function (e) {
    var that = this
  },

  Click: function(e){
    var indexes=this.data.indexes;
    var ans=e.currentTarget.dataset.hi;
    var classid = e.currentTarget.id;
    var correct=this.data.correct;
    var wrong=this.data.wrong;
    
    if(ans==this.data.array.correctanswer){
      correct++;
      this.data.classans[classid]="right";
      wx.showToast({
        title: "回答正确",
        icon: 'none',
        mask:true,
        duration: 1000
      });
    }
    else{
        wrong++;
        var i=this.data.array.answer.indexOf(this.data.array.correctanswer);
        this.data.classans["class"+i]="right";
        this.data.classans[classid]="error";
      wx.showToast({
        title: "回答错误",
        icon: 'none',
        mask: true,
        duration: 1000
      });
    }
    var classans=this.data.classans;
    this.setData({

      classans: classans,
      correct: correct,
      wrong: wrong
    });
    setTimeout(this.next,1000);

    
    app.globalData.corrects = correct;
    app.globalData.wrongs = wrong;
  },
  next:function(){
    var indexes = this.data.indexes;
    indexes++;
    if (indexes == this.data.indexs){
      wx.navigateTo({
        url: '../submit/submit'
      });
    }
    else{
      var that = this;
      this.setData({
        indexes: indexes,
        flag: 0,
        classans: { class0: "answer", class1: "answer", class2: "answer", class3: "answer" }
      });
      wx.request({
        url: 'https://tally.slickghost.com/demo2/getProblem',
        data: { level: app.globalData.lever },
        method: 'get',
        success: function (res) {
          var array = {
            Question: '',
            answer: [],
            correctanswer: ''
          };
          array.Question = res.data[0] + "=?";
          for (var i = 2; i < 6; i++) {
            array.answer.push(res.data[i]);
          }
          array.correctanswer = res.data[1];
          that.setData({
            array: array
          });
        }

      });
    }
  }
})
